package tran;


import org.apache.rocketmq.client.producer.LocalTransactionState;
import org.apache.rocketmq.client.producer.TransactionListener;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.common.message.MessageExt;

import java.nio.charset.StandardCharsets;

/**
 * @BelongsProject: rocketmq-test
 * @BelongsPackage: cn.itsource._05_tran
 * @Author: Director
 * @CreateTime: 2022-07-23 21:48
 * @Description: 定义事务监听器
 * @Version: 1.0
 */
public class MyTranListener implements TransactionListener {


    @Override
    public LocalTransactionState executeLocalTransaction(Message message, Object arg) {
        try {
            System.out.println("执行本地事务"+new String(message.getBody(), StandardCharsets.UTF_8));
            System.out.println(1/0);
            // 保存成功
            return LocalTransactionState.COMMIT_MESSAGE;
        } catch (Exception e) {
            e.printStackTrace();
            return LocalTransactionState.ROLLBACK_MESSAGE;
        }
    }

    @Override
    public LocalTransactionState checkLocalTransaction(MessageExt messageExt) {
        try {
            System.out.println("本地事务查询：提交成功！");
            return LocalTransactionState.COMMIT_MESSAGE;
        } catch (Exception e) {
            e.printStackTrace();
            return LocalTransactionState.COMMIT_MESSAGE;
        }
    }
}
